package pro.bbs.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.yy.kit.string.YYStrKits;
import com.yy.pojo.bbs.TPost;
import com.yy.pojo.bbs.TPostExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pro.bbs.server.mapper.bbs.TPostMapper;
import pro.bbs.server.service.ITPostService;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 论坛帖子服务层
 * </p>
 *
 * @author 杨阳
 * @since 2017年10月12日
 */
@Service(value = "iTPostService")
public class TPostServiceImpl implements ITPostService {

    private final TPostMapper tPostMapper;

    @Autowired
    public TPostServiceImpl(TPostMapper tPostMapper) {
        this.tPostMapper = tPostMapper;
    }

    @Override
    public List<TPost> getAllPost(Map<String, Object> params) {

        String title = (String) params.get("title");
        String content = (String) params.get("content");
        String page = (String) params.get("page");
        String limit = (String) params.get("limit");

        TPostExample example = new TPostExample();
        TPostExample.Criteria criteria = example.createCriteria();

        if (YYStrKits.isNotBlank(title)) {
            criteria.andTitleLike(YYStrKits.like(title));
        }

        if (YYStrKits.isNotBlank(content)) {
            criteria.andContentLike(YYStrKits.like(content));
        }

        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        // 在上面执行了PageHelper.startPage(currentPage, pageSize)，那么紧跟代码的查询方法会被执行分页操作
        List<TPost> postList = tPostMapper.selectByExample(example);

        return postList;
    }
}
